package com.quan.upms.aspect;

import java.lang.reflect.Method;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.quan.base.common.utils.IpHelper;
import com.quan.sso.common.pojo.SSOUser;
import com.quan.sso.common.tools.UserHolder;
import com.quan.upms.annotation.RequiresPermissions;

@Component
@Aspect
public class AccessLogAspect {

	private static final Logger LOGGER = LoggerFactory.getLogger(AccessLogAspect.class);

	// 开始时间
	private long startTime = 0L;

	// 结束时间
	private long endTime = 0L;

	@Before("execution(* *..controller..*.*(..))")
	public void doBeforeInServiceLayer(JoinPoint joinPoint) {
		startTime = System.currentTimeMillis();
	}

	@After("execution(* *..controller..*.*(..))")
	public void doAfterInServiceLayer(JoinPoint joinPoint) {
	}

	@Around("execution(* *..controller..*.*(..))")
	public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
		// 获取request
		RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
		ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
		HttpServletRequest request = servletRequestAttributes.getRequest();
		
		//当前登录用户信息
		SSOUser user = UserHolder.getUser();
		
		// 从注解中获取操作名称、获取响应结果
		Object result = pjp.proceed();
		
		Signature signature = pjp.getSignature();
		MethodSignature methodSignature = (MethodSignature) signature;
		Method method = methodSignature.getMethod();

		//权限注解
		RequiresPermissions annotation = method.getAnnotation(RequiresPermissions.class);
		
		endTime = System.currentTimeMillis();
		LOGGER.info("用户:{}, 操作:{}, IP:{}, URI:{}, 耗时:{}(ms), UserAgent:{}",
				user != null ? user.getAccount() : "" ,
				(annotation != null && !annotation.description().equals("") ? annotation.description() : "未注释"),
				IpHelper.getIpAddr(request),
				request.getRequestURI(),
				endTime - startTime,
				request.getHeader("User-Agent")
				);
		return result;
	}

}
